﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;

using LexaScopeNET;

namespace Lexalytics4Excel.Dialogs
{
    /// <summary>
    /// Text analytics options dialog
    /// </summary>
    public partial class Options : Form
    {
        // URL and Account are assumed to be valid
        private bool _ValidProfile = true;
        Profiles _Profiles = new Profiles();

        /// <summary>
        /// Dialog constructor
        /// </summary>
        public Options()
        {
            InitializeComponent();
            OptionsTabContainer.SelectedTab = OptionsTabContainer.TabPages[0];
        }

        /// <summary>
        /// Override constructor used to set tab for initial settings needed
        /// </summary>
        /// <param name="InitialSettings">True/false indication of initial settings needed</param>
        public Options(bool InitialSettings)
        {
            InitializeComponent();
            if (InitialSettings)
                OptionsTabContainer.SelectedTab = OptionsTabContainer.TabPages[1];
            else
                OptionsTabContainer.SelectedTab = OptionsTabContainer.TabPages[0];
        }

        /// <summary>
        /// Loads the settings for application options
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Options_Load(object sender, EventArgs e)
        {
            // General options
            TopNumberSpinner.Maximum = 100;
            TopNumberSpinner.Minimum = 5;
            if (Lexalytics4Excel.Properties.Settings.Default["Top_N"] != null)
                TopNumberSpinner.Value = Convert.ToInt32(Lexalytics4Excel.Properties.Settings.Default["Top_N"]);
            else
                TopNumberSpinner.Value = 7;

            if (Lexalytics4Excel.Properties.Settings.Default["RowColoring"] != null)
                RowColoringCheckbox.Checked = Convert.ToBoolean(Lexalytics4Excel.Properties.Settings.Default["RowColoring"]);
            else
                RowColoringCheckbox.Checked = false;

            if (Lexalytics4Excel.Properties.Settings.Default["NeutralUpper"] != null)
                udPositive.Value = Convert.ToDecimal(Lexalytics4Excel.Properties.Settings.Default["NeutralUpper"]);
            else
                udPositive.Value = Convert.ToDecimal(0.5);

            if (Lexalytics4Excel.Properties.Settings.Default["NeutralLower"] != null)
                udNegative.Value = Convert.ToDecimal(Lexalytics4Excel.Properties.Settings.Default["NeutralLower"]);
            else
                udNegative.Value = Convert.ToDecimal(-0.5);

            if (Lexalytics4Excel.Properties.Settings.Default["WriteLog"] != null)
                LogFileCheckbox.Checked = Convert.ToBoolean(Lexalytics4Excel.Properties.Settings.Default["WriteLog"]);
            else
                LogFileCheckbox.Checked = false;

            // Profile options
            if (Lexalytics4Excel.Properties.Settings.Default["Profiles"] != null)
            {
                _Profiles = new Profiles((StringCollection)Lexalytics4Excel.Properties.Settings.Default["Profiles"]);
                _ValidProfile = _Profiles.Count>0;
                // Update the list box
                ProfileList.DataSource = _Profiles.ProfileNames;
            }
            else
            {
                _ValidProfile = false;
            }

            // Set dialog button states
            SetFormButtons();
        }

        /// <summary>
        /// Private method to update dialog buttons based on actions in the dialog
        /// </summary>
        private void SetFormButtons()
        {
            // Main form button
            OKButton.Enabled = (_Profiles.Count > 0);

            // Profile tab buttons
            NewProfile.Enabled = true;
            EditProfile.Enabled = ((ProfileList.Items.Count>0) && (ProfileList.SelectedIndex>=0));
            RemoveProfile.Enabled = ((ProfileList.Items.Count > 1) && (ProfileList.SelectedIndex >= 0));
        }

        #region Button handlers

        /// <summary>
        /// Cancel button handler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// OK button handler, persists current option choices to application settings
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OKButton_Click(object sender, EventArgs e)
        {
            // Persist the selection of top N and profiles
            Lexalytics4Excel.Properties.Settings.Default["Top_N"] = Convert.ToInt32(TopNumberSpinner.Value);
            Lexalytics4Excel.Properties.Settings.Default["RowColoring"] = RowColoringCheckbox.Checked;
            Lexalytics4Excel.Properties.Settings.Default["NeutralUpper"] = udPositive.Value;
            Lexalytics4Excel.Properties.Settings.Default["NeutralLower"] = udNegative.Value;
            Lexalytics4Excel.Properties.Settings.Default["Profiles"] = _Profiles.GetProfileCollection();
            Lexalytics4Excel.Properties.Settings.Default["WriteLog"] = LogFileCheckbox.Checked;
            Lexalytics4Excel.Properties.Settings.Default.Save();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        #endregion // Button handlers

        #region Profile button handlers
        /// <summary>
        /// Event handler for new profile button push, displays profile information dialog for information entry.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewProfile_Click(object sender, EventArgs e)
        {
            // Display dialog to enter profile components
            ProfileContents ProfileDlg = new ProfileContents(_Profiles.ProfileNames);
            if (ProfileDlg.ShowDialog() == DialogResult.OK)
            {
                // Fake the return from the dialog box
                Profile myProfile = new Profile() { ProfileName = ProfileDlg.ProfileName, ServiceURL = ProfileDlg.ServiceURL, PublicKey = ProfileDlg.PublicKey, PrivateKey = ProfileDlg.PrivateKey, LocalProfile = ProfileDlg.LocalProfile, LicensePath = ProfileDlg.LicensePath, DataDirectory = ProfileDlg.DataDirectory, UserDirectory = ProfileDlg.UserDirectory};
                _Profiles.Add(myProfile.ProfileName, myProfile);

                // Update the list box
                ProfileList.DataSource = _Profiles.ProfileNames;
                // Update dialog controls
                SetFormButtons();
            }
        }

        /// <summary>
        /// Retrieves information for selected profile and populates profile edit dialog
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditProfile_Click(object sender, EventArgs e)
        {
            string strProfileName = ProfileList.SelectedValue.ToString();
            Profile myProfile = (Profile)_Profiles[strProfileName];
            ProfileContents ProfileDlg = new ProfileContents(myProfile.ProfileName, myProfile.LocalProfile, myProfile.ServiceURL, myProfile.PublicKey, myProfile.PrivateKey, myProfile.LicensePath, myProfile.DataDirectory, myProfile.UserDirectory);
            if (ProfileDlg.ShowDialog() == DialogResult.OK)
            {
                myProfile.LocalProfile = ProfileDlg.LocalProfile;
                myProfile.ServiceURL = ProfileDlg.ServiceURL;
                myProfile.PublicKey = ProfileDlg.PublicKey;
                myProfile.PrivateKey = ProfileDlg.PrivateKey;
                myProfile.LicensePath = ProfileDlg.LicensePath;
                myProfile.DataDirectory = ProfileDlg.DataDirectory;
                myProfile.UserDirectory = ProfileDlg.UserDirectory;
                _Profiles.Remove(myProfile.ProfileName);
                _Profiles.Add(myProfile.ProfileName, myProfile);

                // Update the list box
                ProfileList.DataSource = _Profiles.ProfileNames;
                // Update dialog controls
                SetFormButtons();
            }
        }


        /// <summary>
        /// Removes selected profile and associated information.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RemoveProfile_Click(object sender, EventArgs e)
        {
            string strProfileName = ProfileList.SelectedValue.ToString();
            _Profiles.Remove(strProfileName);
            // Update the list box
            ProfileList.DataSource = _Profiles.ProfileNames;
            // Update dialog controls
            SetFormButtons();
        }

        #endregion
    }
}
